// pages/my/my_card/index.js
const bindingCard=require("../../controllers/ControllerX").controller.bindingCard;
const searchCard=require("../../controllers/ControllerX").controller.searchCard;
const updateCard=require("../../controllers/ControllerX").controller.updateCard;
const getCode=require("../../controllers/ControllerX").controller.getCode;

const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src1:" https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/my/img/my_but_15.png",
    src2:" https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/my/img/my_but_26.png",
    select_src:" https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/my/img/my_but_15.png",
    state:"1",
    select:false,
    cardId:"",
    value:{
      bankNick:"",
      bankName:"",
      bankAddress:"",
      bankAccount:"",
      mobile:"",
      code:"",
      state:0,
      shopId:"",
      id:""
    },
    selectState:false,
    binded:false,
    second:60
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.searchCard()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // },
  // 点击选中
  access(event){
    let state=event.currentTarget.dataset.state;
    if(state=="1"){
      this.setData({
        select_src:this.data.src2,
        state:"0",
        select:true
      })
    }else{
      this.setData({
        select_src:this.data.src1,
        state:"1",
        select:false
      })
    }
  },
  //获取持卡人姓名
  getPersonName(e){
    this.setData({
      'value.bankNick':e.detail.value
    })
  }
  ,
  //获取银行卡号
  getBankNumber(e){
    this.setData({
      'value.bankAccount':e.detail.value
    })
  }
  ,
  //获取开户银行
  getBankName(e){
    this.setData({
      'value.bankName':e.detail.value
    })
  }
  ,
  //获取开户支行
  getBankTrName(e){
    this.setData({
      'value.bankAddress':e.detail.value
    })
  }
  ,
  //获取手机号
  getPhone(e){
    this.setData({
      'value.mobile':e.detail.value
    })
  }
  ,
  //获取验证码
  getCode(e){
    this.setData({
      'value.code':e.detail.value
    })
  }
  ,
  //绑定银行卡
  // 获取银行卡数据
  searchCard(){
    let shopId=wx.getStorageSync('shopId');
    searchCard({shopId:shopId}).then(res=>{
      if(!res.data){
        this.setData({
          selectState:true
        })
      }else{
        this.setData({
          cardId:res.data.id
        })
        this.setData({
         'value.bankNick':res.data.bankNick,
         'value.bankName':res.data.bankName,
         'value.bankAddress':res.data.bankAddress,
         'value.bankAccount':res.data.bankAccount,
         'value.mobile':res.data.mobile
        })
      }
    })
  },
  //修改银行卡
  updateCard(){
    let shopId=wx.getStorageSync('shopId');
    this.setData({
      'value.shopId':shopId,
      'value.id':this.data.cardId
    })
    updateCard(this.data.value).then(res=>{
      if(res.code==1){
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 1000
        })
        setTimeout(function(){
          wx.navigateBack()
        },1000)
      }
    })
  },
  //添加银行卡
  addCard(){
    let shopId=wx.getStorageSync('shopId');
    this.setData({
      'value.shopId':shopId,
      'value.bankAccountId':this.data.cardId
    })
    bindingCard(this.data.value).then(res=>{
      if(res.code==1){
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 1000
        })
        setTimeout(function(){
          wx.navigateBack()
        },1000)
      }
    })
  },

  //执行绑定操作
  BindingCard(){
    if(this.data.selectState==true){
      this.addCard()
    }else{
      this.updateCard()
    }
  },

  //获取验证码
  getGetCode(){
   if(this.data.second==60){
    let mobile=this.data.value.mobile;
    let type="Bank";
    let sendType=2;
    this.setData({
      binded:true
    })
    getCode({mobile:mobile,type:type,sendType:sendType}).then(res=>{
      if(res.code==1){
        wx.showToast({
          icon:"none",
          title: '验证码已发送',
        })
        let timer=setInterval(()=>{
          this.setData({
            second:this.data.second-1
          })
          if(this.data.second<=0){
            this.setData({
              second:60
            })
            clearInterval(timer)
          }
        },1000)
      }
    })
   }
  }
})